package towerDefense.projectiles;

import java.awt.image.BufferedImage;
import towerDefense.towers.CircleSprite;
import towerDefense.towers.TargettingSprite;
import com.golden.gamedev.object.Sprite;


public class TrackingSprite extends TargettingSprite implements Cloneable
{

    public TrackingSprite (BufferedImage image,
                           double x,
                           double y,
                           double r,
                           double spd,
                           Sprite s)
    {
        super(image, x, y, r, spd, s);
    }


    public TrackingSprite (BufferedImage image, double r, double spd, Sprite s)
    {
        super(image, r, spd, s);
    }


    @Override
    public void update (long l)
    {
        super.update(l);
        if (myTarget != null)
        {
            this.updateMovement(l);
            if (moveTo(l,
                       ((CircleSprite) myTarget).getCenterX(),
                       ((CircleSprite) myTarget).getCenterY(),
                       this.getConstantSpeed())) doHitTarget();

        }
    }


    public void doHitTarget ()
    {
        this.setActive(false);
    }


    @Override
    public boolean moveTo (long elapsedTime, double xs, double ys, double speed)
    {
        if (this.getCenterX() == xs && this.getCenterY() == ys)
        {
            return true;
        }

        double angle =
            90 + Math.toDegrees(Math.atan2(ys - this.getCenterY(),
                                           xs - this.getCenterX()));
        double radians = Math.toRadians(angle);

        double vx = Math.sin(radians) * speed * elapsedTime, vy =
            -Math.cos(radians) * speed * elapsedTime;

        boolean arriveX = false, arriveY = false;

        // checking x coordinate
        if (vx != 0)
        {
            if (vx > 0)
            {
                // moving right
                if (this.getCenterX() + vx >= xs)
                {
                    vx = xs - this.getCenterX(); // snap
                    arriveX = true;
                }

            }
            else
            {
                // moving left
                if (this.getCenterX() + vx <= xs)
                {
                    vx = xs - this.getCenterX(); // snap
                    arriveX = true;
                }
            }

        }
        //cushion by 5 pixels
        else if (this.getCenterY() <= xs + 5 && this.getCenterY() >= xs - 5)
        {
            arriveX = true;
        }

        // checking y coordinate
        if (vy != 0)
        {
            if (vy > 0)
            {
                // moving down
                if (this.getCenterY() + vy >= ys)
                {
                    vy = ys - this.getCenterY(); // snap
                    arriveY = true;
                }

            }
            else
            {
                // moving up
                if (this.getCenterY() + vy <= ys)
                {
                    vy = ys - this.getCenterY(); // snap
                    arriveY = true;
                }
            }

        }
        //cushion by 5 pixels
        else if (this.getCenterY() <= ys + 5 && this.getCenterY() >= ys - 5)
        {
            arriveY = true;
        }

        this.move(vx, vy);

        return (arriveX && arriveY);
    }

}
